#define MOTOR_PORT 15
#define MOTOR_STOP 128

#define LED_IR_ENCODER 8

/// <summary>Gets motor status.</summary>
byte IsMotorInitialized;

/// <summary>Initializes the motor.</summary>
void InitializeMotor(void)
{
    // Turn on encoder led
    TurnOn(LED_IR_ENCODER);

    // Turn on motor
    Port_DataDirBit(MOTOR_PORT, PORT_OUT);
    ShutdownMotor();
    Timer_T1PWMX(256, 128, 128, PS_8);
    StopMotors();
    TurnOn(MOTOR_PORT);

    IsMotorInitialized = 1;
}

/// <summary>Shuts down the motor.</summary>
void ShutdownMotor(void)
{
    TurnOff(MOTOR_PORT);

    IsMotorInitialized = 0;
}

/// <summary>Starts the left motor with the specified power.</summary>
/// <param name="power">The power to use.</param>
void StartLeftMotor(byte power)
{
    Timer_T1PWA(power);
}

/// <summary>Starts the right motor with the specified power.</summary>
/// <param name="power">The power to use.</param>
void StartRightMotor(byte power)
{
    Timer_T1PWB(power);
}

/// <summary>Starts both motors with the specified powers.</summary>
/// <param name="left">Power for the left motor.</param>
/// <param name="right">Power for the right motor.</param>
void StartMotorsLR(byte left, byte right)
{
    StartLeftMotor(left);
    StartLeftMotor(right);
}

/// <summary>Starts both motors with the same power.</summary>
/// <param name="power">The power to use for both motors.</param>
void StartMotors(byte power)
{
    StartMotorsLR(power, power);
}

/// <summary>Stops the left motor.</summary>
void StopLeftMotor(void)
{
    Timer_T1PWA(MOTOR_STOP);
}

/// <summary>Stops the right motor.</summary>
void StopRightMotor(void)
{
    Timer_T1PWB(MOTOR_STOP);
}

/// <summary>Stops both motors.</summary>
void StopMotors(void)
{
    StopLeftMotor();
    StopRightMotor();
}